package android.slc.icr.core.room.studio;


import android.slc.icr.core.constant.IcrConstants;
import android.slc.icr.core.entity.IcrChartRoom;
import android.slc.icr.core.entity.IcrMessage;
import android.slc.icr.core.entity.IcrUserInfo;
import android.slc.icr.core.room.manager.IDestroyRoomListener;
import android.slc.icr.core.service.IcrChartRoomMemberService;
import android.slc.icr.core.utils.IcrMessageUtils;
import android.slc.icr.service.IcrMessageRemoteService;
import android.slc.utils.commonlibrary.GsonUtils;

import java.util.Objects;

public class LiveChartRoomStudio extends AbsChartRoomStudio {
    public LiveChartRoomStudio(IcrChartRoom icrChartRoom,
                               IcrMessageRemoteService icrMessageRemoteService,
                               IcrChartRoomMemberService icrChartRoomMemberService,
                               IDestroyRoomListener destroyRoomListener) {
        super(icrChartRoom, icrMessageRemoteService, icrChartRoomMemberService, destroyRoomListener);
    }

    /**
     * 操作事件
     *
     * @param icrMessage
     */
    @Override
    public void onOperateEvent(IcrMessage icrMessage) {
        String action = icrMessage.getAction();
        String sendId = icrMessage.getSender();
        switch (action) {
            //开始上麦
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_START:
                IcrUserInfo icrUserInfoActorStart = ensureJoinRoom(sendId, icrMessage.getContent(), true);
                if (icrUserInfoActorStart == null) {
                    //缺少参数
                    IcrMessageUtils.sendActionMissingParam(getIcrMessageRemoteService(), action, createMustInfo(getIcrChartRoomId(), sendId));
                    break;
                }
                //如果申请上麦的是主播则服务同意上麦权限
                if (Objects.equals(icrUserInfoActorStart.getUserId(), getIcrChartRoom().getCreateUserId())) {
                    icrUserInfoActorStart.setActorStatus(IcrUserInfo.ACTOR_STATUS_AGREE);
                }
                if (icrUserInfoActorStart.getActorStatus() != IcrUserInfo.ACTOR_STATUS_AGREE) {
                    //缺少参数或没有权限
                    IcrMessageUtils.sendActionMissingParam(getIcrMessageRemoteService(), action, "没有上麦权限", createMustInfo(sendId, sendId));
                    break;
                }
                IcrMessage icrMessageActorStart = fillMustInfo(getIcrChartRoomId(), sendId,
                        IcrMessage.newBuilder()
                                .setAction(IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_START)
                                .setContent(GsonUtils.toJson(icrUserInfoActorStart))
                                .build());
                //上麦中则不通知
                if (icrUserInfoActorStart.isActor()) {
                    getIcrMessageRemoteService().sendMessage(icrMessageActorStart);
                    break;
                }
                icrUserInfoActorStart.setActor(true);
                getIcrChartRoomMemberService().updateNumber(icrUserInfoActorStart);
                getIcrMessageRemoteService().sendMessage(icrMessageActorStart);
                sendMsg2AllMember(IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_JOIN, icrUserInfoActorStart);
                break;
            //停止上麦
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_STOP:
                IcrUserInfo icrUserInfoActorStopCache = getIcrChartRoomMemberService().findUserInfo(getIcrChartRoomId(), sendId);
                if (icrUserInfoActorStopCache == null) {
                    //缺少参数或没有权限
                    IcrMessageUtils.sendActionMissingParam(getIcrMessageRemoteService(), action, createMustInfo(sendId, sendId));
                    break;
                }
                ensureActorLeft(icrUserInfoActorStopCache, false);
                getIcrMessageRemoteService().sendMessage(fillMustInfo(getIcrChartRoomId(), sendId,
                        IcrMessage.newBuilder()
                                .setAction(IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_STOP)
                                .setContent(GsonUtils.toJson(formatUserInfo(icrUserInfoActorStopCache)))
                                .build()));
                sendMsg2AllMember(IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_LEFT, icrUserInfoActorStopCache);
                break;
            //申请加入连麦
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_APPLY:
                IcrUserInfo icrUserInfoActorApply = defineUserInfo(sendId, icrMessage.getContent(), false);
                if (icrUserInfoActorApply != null && !icrUserInfoActorApply.isActor()) {
                    getIcrMessageRemoteService().sendMessage(fillMustInfo(sendId, getIcrChartRoom().getCreateUserId(), IcrMessage.newBuilder()
                            .setAction(IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_APPLY)
                            .setContent(GsonUtils.toJson(icrUserInfoActorApply))
                            .build()));
                }
                break;
            //处理申请加入连麦请求
            case IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_APPLY_HANDLER:
                IcrUserInfo icrUserInfoActorApplyHandler = GsonUtils.fromJson(icrMessage.getContent(),
                        IcrUserInfo.class);
                IcrUserInfo defineUserInfo = defineUserInfo(icrUserInfoActorApplyHandler, false);
                if (defineUserInfo == null || !defineUserInfo.selfTest()) {
                    break;
                }
                defineUserInfo.setActorStatus(icrUserInfoActorApplyHandler.getActorStatus());
                getIcrChartRoomMemberService().updateNumber(defineUserInfo);

                getIcrMessageRemoteService().sendMessage(fillMustInfo(getIcrChartRoomId(), defineUserInfo.getUserId(), IcrMessage.newBuilder()
                        .setAction(IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_APPLY_RESULT)
                        .setContent(GsonUtils.toJson(defineUserInfo)).build()));
                break;
            //退出聊天室
            case IcrConstants.Action.ACTION_CHART_ROOM_LEFT:
                IcrUserInfo icrUserInfoLeft = getIcrChartRoomMemberService().findUserInfo(getIcrChartRoomId(), sendId);
                ensureActorLeft(icrUserInfoLeft, true);
                break;
        }
        super.onOperateEvent(icrMessage);
    }

    /**
     * 确保停止了上麦
     */
    private void ensureActorLeft(IcrUserInfo icrUserInfoActorLeft, boolean notifyActorLeft) {
        if (icrUserInfoActorLeft == null) {
            //不在房间内
            return;
        }
        icrUserInfoActorLeft.setActor(false);
        icrUserInfoActorLeft.setActorStatus(IcrUserInfo.ACTOR_STATUS_NONE);
        getIcrChartRoomMemberService().updateNumber(icrUserInfoActorLeft);
        if (notifyActorLeft) {
            sendMsg2AllMember(IcrConstants.Action.ACTION_CHART_ROOM_ACTOR_LEFT, formatUserInfo(icrUserInfoActorLeft));
        }
    }

    /**
     * 销毁
     */
    @Override
    public void destroy() {
        super.destroy();
        getIcrChartRoomMemberService().removeAll(getIcrChartRoomId());
    }
}
